package DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import to.Cargo;
import to.Cargo;
import util.HibernateUtil;

public class CargoDAO {
	public List<Cargo> buscaTodos() throws Exception {
		List<Cargo> listaRetorno = null;
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Cargo.class);
			criteria.addOrder(Order.asc("descricao"));
			listaRetorno = (List<Cargo>) criteria.list();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		return listaRetorno;
	}
	public Cargo pesquisarPorCodigo(Integer codigo) throws Exception{
		Cargo cargo = null;
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			cargo = (Cargo) session.get(Cargo.class, codigo);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}	
		return cargo;
	}
}
